package com.example.mqttmanage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mqttmanage.config.JwtUtil;
import com.example.mqttmanage.config.security.SecurityUtils;
import com.example.mqttmanage.domain.base.ResponseResult;
import com.example.mqttmanage.domain.user.User;
import com.example.mqttmanage.mapper.UserMapper;
import com.example.mqttmanage.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.Map;


@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private JwtUtil jwtUtil;


    @Override
    public ResponseResult<User> login(String username, String password) {
        // 1. 参数校验
        if (!StringUtils.hasText(username) || !StringUtils.hasText(password)) {
            return ResponseResult.error("用户名或密码不能为空");
        }

        // 2. 查询用户
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, username);
        User user = this.getOne(queryWrapper);

        if (user == null) {
            return ResponseResult.error("用户不存在");
        }

        // 3. 验证密码 (假设数据库中存储的是 BCrypt 哈希值)
        if (!passwordEncoder.matches(password, user.getPassword())) {
            return ResponseResult.error("密码错误");
        }


        System.out.println("登录名为"+SecurityUtils.getCurrentUsername());
        // 4. 登录成功，返回用户信息
        return ResponseResult.success(user);
    }

    @Override
    public ResponseResult<String> register(User user) {
        // 1. 参数校验
        if (!StringUtils.hasText(user.getUsername()) || !StringUtils.hasText(user .getPassword())) {
            return ResponseResult.error("用户名和密码不能为空");
        }

        // 2. 检查用户名是否已存在
        LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(User::getUsername, user.getUsername());
        User existUser = getOne(queryWrapper,false);
        if (existUser != null) {
            return ResponseResult.error("用户名已存在");
        }

        // 3. 密码加密 (这里假设前端传入的是明文密码，实际应用中应在 Service 层加密)
        // 注意：如果前端直接传哈希后的密码，请移除下面的加密逻辑
        String plainPassword = user.getPassword(); // 假设这是明文密码
        String hashedPassword = passwordEncoder.encode(plainPassword);
        user.setPassword(hashedPassword);

        // 4. 设置默认角色 (如果需要)
        if (user.getRole() == null) {
            user.setRole("user"); // 默认为普通用户
        }

        // 5. 保存用户
        int saveResult = userMapper.insert(user);
        if (saveResult>0) {
            return ResponseResult.success("注册成功");
        } else {
            return ResponseResult.error("注册失败");
        }
    }
}